var app = getApp();
Page({
  data: {
    //所有的甜点类型以及每个类型下的所有甜点
    goodsList: [],
    shop: {
      dpname: '爱吃甜品的猫',
      desc: '营业时间    08:00-21:00',
      name: '爱吃甜品店'
    },
    //购物车
    cart: {
      count: 0,
      total: 0,
      list: { key:value,key2:2,
      },
      typeIdsArr: {},
      dessertIdsArr: {}
    },
    showCartDetail: false
  },

  goodsListAjsx: function (Url, Type, Datetype) {
    wx.request({
      //查询类型的地址
      url: Url,
      type: Type,
      dataType: Datetype,
      success: res => {
        console.log(res.data.data);
        this.setData({
          goodsList: res.data.data
        })

        this.setData({
          dessertSeleted: this.data.goodsList[0].desserttypeEntity.typeId
        });
      }
    })
  },
  //当页面加载的时候显示甜品类型
  onLoad: function () {
    this.goodsListAjsx('http://localhost:8080/dessertByDessertType.action', 'post', 'json');
  },
  onShow: function () {

  },

  //加入购物车
  tapAddCart: function (e) {
    this.addCart(e.target.dataset.ida);//拿到选择的对象
  },
  //  从购物车中减去
  tapReduceCart: function (e) {
    this.reduceCart(e.target.dataset.ida);
  },
  //加
  addCart: function (dessert) {
    //  得到选择对象的编号和类型编号
    var chooseDessert = dessert.dessertId;
    var chooseType = dessert.dessertType;

    var typeIdAndId = chooseType + "," + chooseDessert;
    //  添加商品到购物车
    this.data.cart.list[typeIdAndId];

    //  得到原本购物车的数量
    var num = this.data.cart.list[typeIdAndId] || 0;
    //  数量+1
    this.data.cart.list[typeIdAndId] = num + 1;

    var goodsList = this.data.goodsList;
    //  便利所有的类型
    for (var i = 0; i <= goodsList.length-1;i++){
      //  得到每一个类型编号
      var allsType = goodsList[i].desserttypeEntity.typeId;
      //  便利每一个类型的所有甜点
      for (var j = 0; j <= goodsList[i].dessert.length-1; j++) {
        //  得到每一个甜点的编号
        var allsDessert = goodsList[i].dessert[j].dessertId;
        //  判断是否和选择的甜点的编号和类型编号一致
        if (allsType == dessert.dessertType && allsDessert == dessert.dessertId){
          //  将类型编号和甜点编号绑定 并保存
          this.data.cart.typeIdsArr[typeIdAndId] = i;
          this.data.cart.dessertIdsArr[typeIdAndId] = j;
          console.log(goodsList[i].dessert[j].dessertName);
        }
      }
    }
    console.log("计算进入购物车计算---");
    this.countCart();
  },
  //减
  reduceCart: function (dessert) {
    var typeIdAndId = dessert.dessertType + "," + dessert.dessertId;
    //  得到商品的编号和类型编号
    var num = this.data.cart.list[typeIdAndId] || 0;
    if (num <= 1) {
      delete this.data.cart.list[typeIdAndId]; //如果购物车中对象小于1就删除对象
      delete this.data.cart.typeIdsArr[typeIdAndId];
      delete this.data.cart.dessertIdsArr[typeIdAndId];
    } else {
      this.data.cart.list[typeIdAndId] = num - 1;//用当前的对象的数量减上num
    }
    this.countCart();
  },
  //计算价格
  countCart: function () {
    var count = 0,
      total = 0;
    for (var typeIdAndId in this.data.cart.list) {
      var cart = this.data.cart;
      //  得到选择商品的数量
      var dessertCount = cart.list[typeIdAndId];
      //  得到选择商品的对象
      var dessert = this.data.goodsList[cart.typeIdsArr[typeIdAndId]].dessert[cart.dessertIdsArr[typeIdAndId]];
      //  计算商品的总数量
      count += dessertCount;
      //  计算商品的总价格
      total += dessert.dessertPrice * dessertCount;
    }

    //赋值
    this.data.cart.count = count;
    this.data.cart.total = total;
    this.setData({
      cart: this.data.cart
    });
  },


  follow: function () {
    this.setData({
      followed: !this.data.followed
    });
  },
  onGoodsScroll: function (e) {
    if (e.detail.scrollTop > 10 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 10 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }

    var scale = e.detail.scrollWidth / 570,
      scrollTop = e.detail.scrollTop / scale,
      h = 0,
      dessertSeleted,
      len = this.data.goodsList.length;
    this.data.goodsList.forEach(function (all, i) {
      var _h = 70 + all.dessert.length * (46 * 3 + 20 * 2);
      if (scrollTop >= h - 100 / scale) {
        dessertSeleted = "a-" + all.dessert[0].dessertType;
      }
      h += _h;
    });
    this.setData({
      dessertSeleted: dessertSeleted
    });
  },
  tapdessert: function (e) {
    var key = e.target.dataset.key;
    this.setData({
      dessertViewed: key
    });
    var self = this;
    setTimeout(function () {
      self.setData({
        dessertSeleted: key
      });
    }, 100);
  },
  showCartDetail: function () {
    this.setData({
      showCartDetail: !this.data.showCartDetail
    });
  },
  hideCartDetail: function () {
    this.setData({
      showCartDetail: false
    });
  },
  formSubmit: function (e) {
  //   server.sendTemplate(e.detail.formId, null, function (res) {
  //     if (res.data.errorcode == 0) {
  //       wx.showModal({
  //         showCancel: false,
  //         title: '恭喜',
  //         content: '订单发送成功！下订单过程顺利完成，本例不再进行后续订单相关的功能。',
  //         success: function (res) {
  //           if (res.confirm) {
  //             wx.navigateBack();
  //           }
  //         }
  //       })
  //     }
  //   }, function (res) {
  //     //console.log(res)
  //   });
  // },
  // goTop: function (e) {
  //   this.setData({
  //     scrollTop: 0
  //   })
  // },
  // scroll: function (e, res) {
  //   // 容器滚动时将此时的滚动距离赋值给 this.data.scrollTop
  //   if (e.detail.scrollTop > 500) {
  //     this.setData({
  //       floorstatus: true
  //     });
  //   } else {
  //     this.setData({
  //       floorstatus: false
  //     });
  //   }

  var allDessert = null;
   for(var key in this.data.cart.list){
     var dessertInfo = null;  
     allDessert[dessertInfo];
   }


    let str = JSON.stringify(this.data.cart);
    wx.navigateTo({
      url: '/pages/confirm/confirm?area='+str
  })
  }
});
